﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Shine.Framework;
using SuperMap.Data;


using System.Runtime.InteropServices;

namespace Shine.POI
{
    public partial class PoiTextbox : UserControl, ICommand
    {
        IApplication m_Application = null;
        public PoiTextbox()
        {
            InitializeComponent();
            textBox1.TextChanged += TextBox1_TextChanged;
          
        }

        private void TextBox1_TextChanged(object sender, EventArgs e)
        {
            (m_Application as IFramework).PoiName = textBox1.Text.Trim();
        }

        #region ICommand Members

        int ICommand.Bitmap
        {
            get { return 0; }
        }

        string ICommand.Caption
        {
            get { return "PoiTextbox"; }
        }

        string ICommand.Category
        {
            get { return "地名"; }
        }

        bool ICommand.Checked
        {
            get { return false; }
        }

        bool ICommand.Enabled
        {
            get
            {
                return true;
            }
        }

        int ICommand.HelpContextID
        {
            get { return 0; }
        }

        string ICommand.HelpFile
        {
            get { return string.Empty; }
        }

        string ICommand.Message
        {
            get { return "PoiTextbox"; }
        }

        string ICommand.Name
        {
            get { return "地名"; }
        }

        void ICommand.OnClick()
        {
        }

        void ICommand.OnCreate(object hook)
        {
          
           
                m_Application = hook as IApplication;
             
        }

     
        string ICommand.Tooltip
        {
            get { return "地名"; }
        }







        #endregion

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode== Keys.Enter)
            {
                string name = textBox1.Text.Trim();
                if (name == "") return;
               
                string poiDatasource = System.Configuration.ConfigurationManager.AppSettings["POI_Datasource"];
                string poiDataset = System.Configuration.ConfigurationManager.AppSettings["POI_Dataset"];
                string poiField = System.Configuration.ConfigurationManager.AppSettings["POI_Field"];
                if (string.IsNullOrEmpty(poiDatasource) == false &
                    string.IsNullOrEmpty(poiDataset) == false &
                    string.IsNullOrEmpty(poiField) == false
                    )
                {
                    Datasource ds = (m_Application as IFramework).WorkspaceControl.WorkspaceTree.Workspace.Datasources[poiDatasource];
                    if (ds == null) return;
                    DatasetVector dst = ds.Datasets[poiDataset] as DatasetVector;
                    if (dst == null) return;
                    Recordset rst = dst.Query(poiField + " like '%" + name + "%'", CursorType.Dynamic);
                    (m_Application as IFramework).DockManager.ActivateDockPanel("查询结果");
                    DevComponents.DotNetBar.Controls.ListViewEx lstview = (m_Application as IFramework).DockManager.GetControlFromDockPanel("查询结果") as DevComponents.DotNetBar.Controls.ListViewEx;
                    int idx = 0;
                    lstview.Items.Clear();
                    rst.MoveFirst();
                    while (!rst.IsEOF)
                    {
                        idx++;
                        ListViewItem item = lstview.Items.Add(idx.ToString());
                        item.SubItems.Add(rst.GetFieldValue(poiField).ToString());
                        Geometry geo = rst.GetGeometry();
                        item.Tag = geo;
                        rst.MoveNext();
                    }

                    rst.Dispose();

                }
            }
        }
    }
}
